﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class Menu_Edit : BasePage
{
    private int menuId;
    protected void Page_Load(object sender, EventArgs e)
    {
        UserPower.CheckAction((int)FreeHouse.Power.Actions.TMenu.Edit);
        int.TryParse(Request.QueryString["menuId"], out menuId);
        if (!IsPostBack)
            BindMenu();
    }
    protected void back_Click(object sender, EventArgs e)
    {
        Response.Redirect("Default.aspx");
    }
    protected void save_Click(object sender, EventArgs e)
    {
        error.Items.Clear();
        FreeHouse.Model.TMenu aMenu = new FreeHouse.Model.TMenu();
        aMenu.MenuName = menuName.Text.Trim();
        aMenu.PageUrl = pageUrl.Text.Trim();
        aMenu.ParentMenuId = int.Parse(parentMenu.SelectedValue);
        aMenu.MenuId = menuId;
        decimal menuSort;
        decimal.TryParse(sort.Text, out menuSort);
        aMenu.Sort = menuSort;

        if (aMenu.MenuName == "")
            error.Items.Add(new ListItem("填写菜单名称."));
        if (aMenu.PageUrl == "")
            error.Items.Add("填写菜单导向Url.");
        if (error.Items.Count > 0) return;
        new FreeHouse.BLL.TMenu().Update(aMenu);
        error.Items.Add(new ListItem("菜单编辑成功."));
    }

    protected void BindMenu()
    {
        DataSet ds = new FreeHouse.BLL.TMenu().GetList("parentMenuid=0 ");
        DataTable dt = ds.Tables[0].Clone();
        foreach (DataRow parentRow in ds.Tables[0].Rows)
        {
            dt.Rows.Add(parentRow.ItemArray);
            DataTable childMenu = new FreeHouse.BLL.TMenu().GetList("parentMenuId=" + parentRow["menuid"].ToString()).Tables[0];
            foreach (DataRow childRow in childMenu.Rows)
            {
                childRow["MenuName"] = Server.HtmlDecode("&nbsp;&nbsp;&nbsp;&nbsp;") + childRow["MenuName"].ToString();
                dt.Rows.Add(childRow.ItemArray);
            }
        }


        parentMenu.DataSource = dt.DefaultView;
        parentMenu.DataValueField = "MenuId";
        parentMenu.DataTextField = "MenuName";
        parentMenu.DataBind();
        parentMenu.Items.Insert(0, new ListItem("请选择...", "0"));

        //绑定menu 数据
        FreeHouse.Model.TMenu model = new FreeHouse.BLL.TMenu().GetModel(menuId);
        if (model != null)
        {
            menuName.Text = model.MenuName;
            pageUrl.Text = model.PageUrl;
            parentMenu.SelectedValue = model.ParentMenuId.ToString();
            sort.Text = model.Sort.ToString();

        }
    }
}
